﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Zy.WPF.CtrLib.Enums;
using Zy.WPF.CtrLib.ViewModels;

namespace Zy.WPF.CtrLib.Windows
{
    /// <summary>
    /// ZyDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ZyDialog : Window
    {
        public ZyDialog(string _message = "")
        {
            InitializeComponent();
            data = new ZyDialogModel(_message);
            InitData();
        }
        public ZyDialog(ZyDialogModel arg)
        {
            InitializeComponent();
            data = arg;
            InitData();
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            tblcTitle.DataContext = data;
            tb_message.DataContext = data;
            tblcMem.DataContext = data;
            string themeName = data.Type.ToString();
            img.Source = new BitmapImage(new Uri($"pack://application:,,,/Zy.WPF.CtrLib;component/Resources/Static/{themeName}.png", UriKind.RelativeOrAbsolute));
            btn_centel.ToolTip = "点击取消";
            this.Resources["colorDefault"] = (Color)this.Resources[$"color{themeName}"];
        }

        /// <summary>
        /// 弹窗数据
        /// </summary>
        readonly ZyDialogModel data;
        /// <summary>
        /// 左键按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
        /// <summary>
        /// 确定按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_enter_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }
        /// <summary>
        /// 取消按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_centel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
        /// <summary>
        /// 展示提示框
        /// </summary>
        /// <returns></returns>
        public new bool? ShowDialog()
        {
            AutoClose();
            return base.ShowDialog();
        }
        /// <summary>
        /// 自动关闭弹窗的方法
        /// </summary>
        private void AutoClose()
        {
            #region 自动关闭弹窗的逻辑
            //启动另一个线程
            Task.Factory.StartNew(() => {
                while (data.CloseTime > 0)
                {
                    data.CloseTime--;
                    Thread.Sleep(1000);
                }
                if (data.CloseTime <= 0)
                {
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        this.Close();
                    }));
                }
            });
            #endregion
        }
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            data.CloseTime = 30;
        }
      
    }
}
